<?php
/**
 * Bootstrap class, sets paths and parses ini file
 * @author Joe Minichino <joe@dsforge.net>
 * 
 */
class Bootstrap {

    function  __construct() {
    }

    function init(){
        // place your initialization code here
        require_once "debug.php";
        require_once 'common.php';
        require_once "controller.php";
        require_once "db.php";
        require_once "model.php";

        $array = parse_ini_file("config.ini",true);
        $param = $array['GENERAL'];
        define('BASE_URL', $param['baseurl']);
        $this->createModels();
        
    }
    
    function createModels(){
        Debug::log("creating models...");
        
        $array = parse_ini_file("config.ini",true);
        $param = $array['MODELS'];
        
        
        
        foreach($param as $table => $object){
            Debug::log("$table => $object");
            $db = DB::get();
            
            $rows = $db->query("describe $table");
            
            
            $fields = "";
            foreach($rows as $row){
                $fields .= ",'$row[0]'";
            }
            $fields = substr($fields,1);
            
            
            $defaults = "";
            foreach($rows as $row){
                if($row['Default']!=''){
                    $defaults .= ", '{$row[0]}' => '{$row['Default']}'";
                }
            }
            $defaults = substr($defaults,1);
            
            $class = <<<PHP
            class $object extends Model {
                public \$table = "$table";
                public \$fields = array($fields); 
                public \$defaults = array($defaults);

            }
PHP;
            Debug::log($class);
            eval($class);
        }
        

        
    }
    
}
